package com.hand.qiandu.aspect;

import com.hand.qiandu.annotation.PermissAnnotation;
import com.hand.qiandu.constant.SysContant;
import com.hand.qiandu.service.IUserService;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
import org.springframework.aop.framework.ReflectiveMethodInvocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import javax.servlet.http.HttpServletRequest;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;


/**
 * @author 24748  刘瑞锋
 * @date 2019/7/18
 */
@Aspect
@Component
public class ServiceAdviece {

//    private Logger logger = LoggerFactory.getLogger(ServiceAdviece.class);
//
//    @Autowired
//    private  IUserService userService;
//    // 切入点
//
//    @Pointcut("execution(* com.hand.qiandu.controller.*.*(..)) " )
//    public void servicePointCut(){
//    };
//
//    //前置通知
//    @Before("servicePointCut()")
//    public void beforeFunction(JoinPoint joinPoint){
//        //可以返回请求的方法，参数等信息
//        logger.info("切面--before");
//    }
//
//    //环绕
//    @Around("servicePointCut()")
//    public Object arounfFunction(ProceedingJoinPoint pJP) throws Throwable {
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        //获取请求
//        HttpServletRequest request = attributes.getRequest();
//        if(isVaild(pJP)){
//            //如果需要校验
//            if(userPermissionValid(request)){
//                //校验成功
//                //如果不执行这句，会不执行切面的Before方法及controller的业务方法
//                Object proceed = pJP.proceed();
//                return proceed;
//            }else {
//                //返回值应该怎么写？？？ 返回值应该为list 与调用方法一致？
//                return "";
//            }
//        }
//        return "error";
//    }
//
//    //后置返回通知
//    @AfterReturning(pointcut = "servicePointCut()", returning = "result")
//    public void afterFunction(Object result){
//        System.out.println("afterReturning");
//    };
//
//    @AfterThrowing(pointcut = "servicePointCut()", throwing = "exception")
//    public void afterThrowing(Throwable exception ){
//        System.out.println("afterThrowing");
//    };
//
//
//
//    //验证uri和path
//    Boolean userPermissionValid(HttpServletRequest request){
//            if(!StringUtils.isEmpty(request.getHeader("userId")) && !StringUtils.isEmpty(request.getMethod())){
//                Long userId = Long.valueOf(request.getHeader("userId"));
//                String uri = request.getRequestURI();
//                String method = request.getMethod();
//                List<Map<String,String>> listMap = userService.queryUserPermission(userId);
//                AntPathMatcher ant = new AntPathMatcher();
//                for(Map<String,String> map : listMap){
//                    if(ant.match(map.get("path"),uri) && map.get("method").equals(method)){
//                        return true;
//                    }
//                }
//            }
//            return false;
//        }
//
//
//
//    //根据是否有注解来判断是否需要验证
//    private boolean isVaild(ProceedingJoinPoint pJP) throws NoSuchFieldException, IllegalAccessException {
//            //转换为handleMethod就可以获取方法上的注解了
//            MethodSignature methodSignature = (MethodSignature) pJP.getSignature();
//            Method method = methodSignature.getMethod();
//            PermissAnnotation permissAnnotation = method.getAnnotation(PermissAnnotation.class);
//            if(null != permissAnnotation){
//                return permissAnnotation.value();
//            }else{
//                PermissAnnotation classAnnonation = method.getDeclaringClass().getAnnotation(PermissAnnotation.class);
//                if(null != classAnnonation){
//                    return classAnnonation.value();
//                }
//            }
//            return false;
//        }
}
